package com.wwdx.controller.rest;

import com.google.common.collect.Maps;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.dto.PurchaseCordDTO;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.utils.ConstantHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.SortedMap;

/**
 * @author liguoliang
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class RestUserController extends BaseController{

    /**
     * 用户已购买记录
     */
    @RequestMapping("/purchase/{token}/{sign}/{timestamp}/{merchantId}")
    @ApiOperation(value = "用户已购列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "merchantId",value = "商户ID",required = true)
    })
    public ResultBean purchase(@PathVariable("token") String token, @PathVariable("sign") String sign,
                               @PathVariable("timestamp") String timestamp,@PathVariable("merchantId") String merchantId){

        SortedMap<String, Object> sortedMap = super.getSortedMap(token, timestamp);
        sortedMap.put("merchantId",merchantId);
        ResultBean resultBean = super.validSignTokenResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = (String) resultBean.getData();
        return renderSuccess(userService.bookshelfList(openId,merchantId));
    }

    /**
     * 添加书架
     * @param productId 产品ID
     */
    @RequestMapping("/bookshelf/add/{token}/{sign}/{timestamp}/{productId}")
    @ApiOperation(value = "添加书架",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "productId",value = "产品ID",required = true)
    })
    public ResultBean bookshelfadd(@PathVariable("token") String token, @PathVariable("sign") String sign,
                                   @PathVariable("productId") Long productId, @PathVariable("timestamp") String timestamp){

        SortedMap<String, Object> sortedMap = super.getSortedMap(token,timestamp);
        sortedMap.put("productId",productId);
        ResultBean resultBean = super.validSignTokenResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = (String) resultBean.getData();
        WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(productId);
        if (wwdxBookModel != null){
            PurchaseCordDTO dto = new PurchaseCordDTO();
            dto.setPayTime(new Date());
            Integer type = wwdxBookModel.getType();
            dto.setType(type == 0 ? ConstantHolder.Constant.BOOKTYPE : ConstantHolder.Constant.SMALLBOOKTYPE);
            dto.setProductId(wwdxBookModel.getBookid());
            dto.setProductName(wwdxBookModel.getBookname());
            dto.setProductImg(wwdxBookModel.getBookimg());
            dto.setProductIntroduce(wwdxBookModel.getBookrecommend());
            String key = String.format(ConstantHolder.USER_BOOKSHELF_MAP,openId);
            hashOperations.put(key,productId.toString(), dto);
        }
        return renderSuccess();
    }

    /**
     * 移除书架
     * @param productId 产品ID
     */
    @RequestMapping("/bookshelf/del/{token}/{sign}/{timestamp}/{productId}")
    @ApiOperation(value = "移除书架",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "productId",value = "产品ID",required = true)
    })
    public ResultBean bookshelfdel(@PathVariable("token") String token, @PathVariable("sign") String sign,
                                   @PathVariable("productId") Long productId, @PathVariable("timestamp") String timestamp){

        SortedMap<String, Object> sortedMap = super.getSortedMap(token,timestamp);
        sortedMap.put("productId",productId);
        ResultBean resultBean = super.validSignTokenResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = (String) resultBean.getData();
        String key = String.format(ConstantHolder.USER_BOOKSHELF_MAP,openId);
        hashOperations.delete(key,productId.toString());
        return renderSuccess();
    }

    /**
     * 查询用户观看书籍数与观看时长
     */
    @RequestMapping("/duration/get/{token}/{sign}/{timestamp}")
    @ApiOperation(value = "查询用户观看书籍数与观看时长",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true)
    })
    public ResultBean durationget(@PathVariable("token") String token, @PathVariable("sign") String sign,
                                  @PathVariable("timestamp") String timestamp){

        ResultBean resultBean = super.validSignTokenResult(sign,super.getSortedMap(token,timestamp));
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = (String) resultBean.getData();
        Map<String,Object> map = Maps.newHashMapWithExpectedSize(2);
        String key = String.format(ConstantHolder.USER_DURATION_LIST,openId);
        map.put("duration",listOperations.size(key));
        map.put("booknum",new HashSet<>(listOperations.range(key,0,-1)).size());
        return renderSuccess(map);
    }

    /**
     * 保存用户观看书籍数与观看时长
     */
    @RequestMapping("/duration/add/{token}/{sign}/{timestamp}/{bookid}")
    @ApiOperation(value = "保存用户观看书籍数与观看时长",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "bookid",value = "书籍ID",required = true)
    })
    public ResultBean durationadd(@PathVariable("token") String token, @PathVariable("sign") String sign,
                                  @PathVariable("bookid") Long bookid, @PathVariable("timestamp") String timestamp){

        SortedMap<String, Object> sortedMap = super.getSortedMap(token,timestamp);
        sortedMap.put("bookid",bookid);
        ResultBean resultBean = super.validSignTokenResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = (String) resultBean.getData();
        String key = String.format(ConstantHolder.USER_DURATION_LIST,openId);
        listOperations.leftPush(key,bookid.toString());
        return renderSuccess();
    }

    /**
     * 查询用户章节播放记录
     */
    @RequestMapping("/videopoint/get/{token}/{sign}/{timestamp}/{chapterid}")
    @ApiOperation(value = "查询用户章节播放记录",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "chapterid",value = "章节ID",required = true)
    })
    public ResultBean videopointget(@PathVariable("token") String token, @PathVariable("sign") String sign,
                                    @PathVariable("timestamp") String timestamp,@PathVariable("chapterid") Long chapterid){

        SortedMap<String, Object> sortedMap = super.getSortedMap(token, timestamp);
        sortedMap.put("chapterid",chapterid);
        ResultBean resultBean = super.validSignTokenResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = (String) resultBean.getData();
        String key = String.format(ConstantHolder.USER_VIDEO_MAP,openId);
        return renderSuccess(hashOperations.get(key, chapterid.toString()));
    }

    /**
     * 保存用户章节播放记录
     */
    @RequestMapping("/videopoint/add/{token}/{sign}/{timestamp}/{chapterid}/{milli}")
    @ApiOperation(value = "保存用户章节播放记录",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "timestamp",value = "时间戳",required = true),
            @ApiImplicitParam(name = "chapterid",value = "章节ID",required = true),
            @ApiImplicitParam(name = "milli",value = "毫秒",required = true)
    })
    public ResultBean videopointadd(@PathVariable("token") String token, @PathVariable("sign") String sign,
                                    @PathVariable("milli") String milli, @PathVariable("chapterid") Long chapterid,
                                    @PathVariable("timestamp") String timestamp){

        SortedMap<String, Object> sortedMap = super.getSortedMap(token,timestamp);
        sortedMap.put("chapterid",chapterid);
        sortedMap.put("milli",milli);
        ResultBean resultBean = super.validSignTokenResult(sign,sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String openId = (String) resultBean.getData();
        String key = String.format(ConstantHolder.USER_VIDEO_MAP,openId);
        hashOperations.put(key,chapterid.toString(),milli);
        return renderSuccess();
    }
}